Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


How to Use Tables

With the JTable(in the API reference documentation) class, you can display tables of data.

Think of JTable as a viewer of your data. JTable has no cache of what data it is displaying. You write an object that provides your data to JTable as the table displays the data. This is done via the EditableTableDataModel(in the API reference documentation) interface. So if as your data values change -- for example, rows are added -- you let the JTable know there's new data via one of the TableModelListener(in the API reference documentation) methods. And JTable then displays the new info.

Here's a little code to set up a table:

// MyData is the object that implements the EditableTableDataModel
// interface that you have to write.
EditableTableDataModel myDataModel = new MyData();


// Create the table
JTable table = new JTable(myDataModel);

// Set up the columns of the table

for (int columnIndex = 0; columnIndex < NUM_COLUMNS; columnIndex++){
    // Create a column object for each column of data
    JTableColumn newColumn = new JTableColumn("Column "+columnIndex);
			// The string parameter to JTableColumn needs
 			// to be unique for each column.  See Javadoc
 			// of JTableColumn for more info
 
     table.addColumn(newColumn);
}


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up